/**
 * Created with Vim7.3 ubuntu12.04
 * @fileOverview : 
 * @author : Lyle <lylechen2014@gmial.com>
 * @since : 2014-08-25 13:07:58
 * @filename : static/nmm/src/views/userList.js
 * @version : 
 * @description : 
 */

define(function(require, exports, module){

    require("{component}dialog/zepto.dialog.css");
    require('{component}promptTip/promptTip.css');

    var visitorsView, userInfo, scrollTop=0, scrollAjax=true,
        mId = 'visitors',reRender = false,menuName = 'visitors',
        condition = {
            page    : 0,
            from    : 0,
            offset  : 20
        },
        imglazyload = require('{component}imglazyload/zepto.imglazyload'),
        panel = require('{component}panel/zepto.panel'),
        promptTip = require('{component}promptTip/promptTip'),
        dialog = require('{component}dialog/zepto.dialog'),
        tpl = require('{view}visitors.tpl'),
        tplN = require('{view}userNav.tpl'),
        tplL = require('{view}visitorsLi.tpl');


    visitorsView = Backbone.View.extend({

        el: '',

        template: _.template(tpl),
        templateN: _.template(tplN),
        templateL: _.template(tplL),

        initialize: function () {},

        render: function (obj) {
            if (!share.checkPermission(true)){return;};  
       
            share.reRender(reRender,mId);

            if ($('#'+mId).length < 1) { 
                $('body').append(this.template(lang));
                this.bindEvt();
                userInfo = share.userInfo();
            } else {
                $('#'+mId).css('display','block');
            }

            share.panel(panel,this.templateN,mId,menuName);
 
            if (share.isGuest()) {
                this.payDialog();
            } else {
                this.closeDialog();
                var dom = $('#'+mId).find('.body_all').find('ul');
                if (dom.find('li').length < 1) {
                    this.ajaxList(condition); 
                } else {
                    (function(y){
                        setTimeout(function() {
                           window.scroll(0,y);
                        },10);
                    })(scrollTop);
                }
            } 
        },

        bindEvt : function() {
            var tthis = this,dom = $('#'+mId);

            $(window).on('scroll.visitors',function(e){
                if(seajs.moduleUI == mId) {
                    var WH = $(window).height(),
                        SH = $(window).scrollTop(),
                        //zetpo position:absolte $(document).height() bug /jquery OK
                        DH = $('#'+mId).height();
                    scrollTop = SH;                  
                    if (SH > 0 && (DH-WH-SH) < 100) { 
                        tthis.ajaxList(condition);
                        //WH = $(window).height();
                        //window.scroll(0,WH);
                    }
                    dom.find('.body_all').find('ul').imglazyload({
                        startY : SH,
                        endY:SH + $(window).height()
                    });
                }
            });

            dom.find('.userList').on('touchstart',function(e) {
                $(this).find('li').css('background','transparent');           
                var li = $(e.target).closest('li');
                li.css('background','#D5DDE0');           
            }).on('touchend',function(e){
                var li = $(e.target).closest('li');
                li.css('background','transparent');           
            });

            $('#backTop').live('tap.visitors',function(){
                window.scroll(0,0); 
                share.backTopEnd($('body'));
            });

            dom.find('.left').on('tap',function(e){
                e.stopPropagation();
                if (panel.info.state == 'hidden') {
                    panel.show();
                } else {
                    panel.hidden(); 
                }
            }); 

            dom.on('tap',function(){
                if (panel.info.state == 'show' && $(window).width() < 640) {
                    panel.hidden();
                }
            });

            dom.find('.userList').on('tap',function(e) {
                var uid = $(e.target).closest('li').attr('uid'),
                    userName = $(e.target).closest('li').attr('username');
                if (uid) {
                    if ($(window).height() < 640) {
                        if (panel.info.state == 'hidden') {
                            window.location.href = '#userProfile/whole/custom'+uid+'&&'+userName; 
                        }
                    } else {
                        window.location.href = '#userProfile/whole/custom'+uid+'&&'+userName; 
                    }
                }
            });
        },

        payDialog : function() {
			var dom=$('#'+mId).find('.body_all').find('ul');
            /*dialog.open({
                type : 'payDialog',
                dom : $('#'+mId).find('.body_all'),
                cover : true,
                positionRelative : true
            });*/
			promptTip.show({type:'noPermission',text:'Upgrade Now to enioy more privileges', dom:dom});
        },
          
        closeDialog : function() {
            dialog.close({
                dom : $('#'+mId)
            }); 
        },

        ajaxList : function(data) {
            var tthis = this , dom = $('#'+mId).find('.body_all').find('ul');
            var obj = {
                url : seajs.data.vars.apiAccessUrl+'my_visitors',
                type : 'POST',
                data : data,
                success : function(data) {
 
                    var Img = seajs.data.vars.resourceUrl+'img/';
                    //var list = $.extend({}, data.un_see, data.had_see);
                    var list = data.had_see.concat(data.un_see);
                    if(list.length < 1 && dom.find('li').length < 1) {
                        tthis.noData(dom);
                        return; 
                    }

                    if (data.un_see.length + data.had_see.length) {
                        $('#'+mId).find('.userList').append(tthis.templateL({arr:list,defaultImg:Img}));
                        dom.imglazyload({startY : 0,endY: $(window).height()}); 
                        condition['page']++;
                        condition['from'] = condition['page'] * condition['offset'];
                        //share.cacheUserList(data.had_see);
                    } else {
                        //tthis.noData(dom);
                    };
                },
                error : function(xhr, errorType, error) {
                },
                complete : function(xhr, status) {
                    share.ajaxEndB($('#'+mId).find('ul'));
                    setTimeout(function(){
                        scrollAjax = true;
                    },500)
                }
            };
            if (scrollAjax) {
                promptTip.hide({type:'noData', dom:dom});
                scrollAjax = false;
                share.ajaxStartB(dom);
                $.ajax(share.ajaxControl(obj));
            }
        },

        noData : function(dom) {
            promptTip.show({type:'noData',text:'When someone visits your profile then\'ll show up here.', dom:dom});
        },
        
    });

    module.exports = visitorsView;
})

